<?php

class Ewallet extends Page_Controller {
    private static $allowed_actions = array(
        'process'
    );

    function process() {
        $payment_type = $this->request->param('OtherID');
        if(SecurityToken::inst()->checkRequest($this->request) && $payment_type && ClassInfo::exists($payment_type)){
            try {
            	DB::getConn()->transactionStart();
                $payment = $payment_type::get()->byID($this->request->param('ID'))->pendingPayment();
                if($payment->checkBalance()){
                    $payment->completePayment();
					DB::getConn()->transactionEnd();
					return $this->redirect($payment->RedirectLink());
                }
                else{
                    $payment->failurePayment(array('Message' => _t('Ewallet.INSUFFICIENT_BALANCE', 'Insufficient account balance')));
					$this->setMessage('error', _t('Ewallet.INSUFFICIENT_BALANCE', 'Insufficient account balance'));
                }
                DB::getConn()->transactionEnd();
				return $this->redirectBack();
            }
            catch(ValidationException $e) {
                DB::getConn()->transactionRollback();
                SS_Log::log(new Exception(print_r($e->getMessage(), true)), SS_Log::NOTICE);
				$this->setMessage('error', $e->getResult()->message());
				return $this->redirectBack();
            }
        }
        
        return $this->httpError('404');
    }
}
